var t = getApp()
Page({
  data: {
    user: '',
    pass: '',
    checked:false,
    usernames:[],
    showlist:false,
    listIndex:0
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onShow: function () {
    // 生命周期函数--监听页面显示
    wx.hideTabBar({})
  },
  onLoad: function () {
    let usernames=wx.getStorageSync("usernames");
    this.setData({
      checked: wx.getStorageSync("checked"),
      usernames:usernames
    })

  },
  // 获取输入账号 
  usernameInput: function (e) {
    this.setData({
      user: e.detail.value
    })
  },
  // 获取输入密码 
  passwordInput: function (e) {
    this.setData({
      pass: e.detail.value
    })
  },
  checkboxChange: function(e){
    console.log(e.detail.value);
    if(e.detail.value=='1'){
      console.log(1);
      wx.setStorageSync("checked",true);
    }else{
      console.log(2);
      wx.setStorageSync("checked",false);
    }
  },
  // 登录处理
  login: function () {
    var that = this;
    let usernames=wx.getStorageSync("usernames");
    let checked=wx.getStorageSync("checked");
    if (that.data.user.length == 0 || that.data.pass.length == 0) {
      wx.showToast({
        title: '账号或密码不能为空',
        icon: 'none',
        duration: 2000
      })
    } else {
      var that=this
      t.request('/Merchant/MerchantSide/login',{user: that.data.user,pass: that.data.pass},function(e){
        console.log(e);
          if (e.code == "200") {
            wx.setStorageSync('user_id', e.result.user.u_id);
            wx.setStorageSync('user', e.result.user);
            if(checked==true){
              if(usernames){
                let ishave=0;
                for(let i=0;i<usernames.length;i++){
                  console.log(usernames[i].user);
                  if(usernames[i].user==that.data.user){
                    usernames[i].pass=that.data.pass;
                    ishave=1;
                  }
                }
                if(ishave==0){
                  let arr={};
                  arr.user=that.data.user;
                  arr.pass=that.data.pass;
                  console.log(arr);
                  usernames.push(arr);
                }
                wx.setStorageSync('usernames', usernames);
              }else{
                let usernames=[];
                let arr={};
                arr.user=that.data.user;
                arr.pass=that.data.pass;
                console.log(arr);
                usernames.push(arr);
                wx.setStorageSync('usernames', usernames);
              }
            }
            wx.navigateTo({
              url: '/pages/index/index',
            })
          } else {
            wx.showToast({
              title: e.message,
              icon: 'none',
              duration: 2000
            })
          }
      })
    }
  },
  onPullDownRefresh: function () {
    console.log('onPullDownRefresh')
    // 3秒模拟数据加载
    setTimeout(function () {
      // 不加这个方法真机下拉会一直处于刷新状态，无法复位
      wx.stopPullDownRefresh()
    }, 2000)
  },
  showlist: function () {
    this.setData({
      showlist:true
    })
  },
  bindlistChange(e){
    console.log('切换改变的值', e.detail.value);
    let index=e.detail.value;
    let username=wx.getStorageSync("usernames");
    console.log(username[index].user);
    this.setData({
      user:username[index].user,
      pass:username[index].pass,
      listIndex:e.detail.value
    })
  },
})